package services

import (
	"errors"
	"lingc/models"
	"time"

	"gorm.io/gorm"
)

// SysContentService 提供账号登录校验的服务
type SysContentService struct {
	DB *gorm.DB
}

// NewSysContentService 创建一个新的 SysContentService 实例
func NewSysContentService(db *gorm.DB) *SysContentService {
	return &SysContentService{DB: db}
}

//表单业务

/**
 * @Description: 分页获取列表
 * @param where
 * @param page
 * @param pageSize
 * @return []*models.SysContent
 * @return int
 * @return int64
 * @return error
 */
func (s *SysContentService) PageSysContentList(where string, page, pageSize int) ([]*models.SysContentList, int, int64, error) {
	var groups []*models.SysContent
	var total int64

	// 计算偏移量
	offset := (page - 1) * pageSize

	// 获取总记录数
	if err := s.DB.Model(&models.SysContent{}).Where(where).Count(&total).Error; err != nil {
		return nil, 1, 0, err
	}

	// 获取分页数据
	if err := s.DB.Where(where).Offset(offset).Limit(pageSize).Order("id DESC").Find(&groups).Error; err != nil {
		return nil, 1, 0, err
	}
	var list []*models.SysContentList
	list = []*models.SysContentList{}
	for _, data := range groups {
		list = append(list, &models.SysContentList{
			ID:        data.ID,
			Lang:      data.Lang,
			Title:     data.Title,
			Type:      data.Type,
			Content:   data.Content,
			Status:    data.Status,
			AppID:     data.AppID,
			CreatedAt: time.Unix(data.CreatedAt, 0).Format("2006-01-02 15:04:05"), // 格式化时间戳
			UpdatedAt: time.Unix(data.UpdatedAt, 0).Format("2006-01-02 15:04:05"), // 格式化时间戳
		})
	}

	return list, page, total, nil
}

/**
 * @Description: 创建新
 * @param username
 * @param password
 * @param realname
 * @param avatar
 * @param status
 * @return *models.SysContentCreate
 * @return error
 */
func (s *SysContentService) CreateSysContent(data *models.SysContentCreate) (*models.SysContentCreate, error) {
	addData := &models.SysContent{
		Lang:      data.Lang,
		Title:     data.Title,
		Type:      data.Type,
		Content:   data.Content,
		Status:    data.Status,
		CreatedAt: time.Now().Unix(),
		UpdatedAt: time.Now().Unix(),
	}
	// 保存到数据库
	if err := s.DB.Create(addData).Error; err != nil {
		return nil, err
	}
	return data, nil
}

// GetSysContent 获取信息
func (s *SysContentService) GetSysContent(where string) (*models.SysContent, error) {
	var data models.SysContent
	if err := s.DB.Where(where).First(&data).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("数据不存在")
		}
		return nil, err
	}
	// log.Printf("data_info:%v", data)
	return &data, nil
}

/**
 * @Description: 更新信息
 * @param data
 * @return error
 */
func (s *SysContentService) UpdateSysContent(data *models.SysContentUpdate) (*models.SysContentUpdate, error) {
	// 检查名是否已存在
	var existingData models.SysContent
	if err := s.DB.Where("id = ?", data.ID).First(&existingData).Error; err != nil {
		return nil, errors.New("数据不存在")
	}
	existingData.Lang = data.Lang
	existingData.Title = data.Title
	existingData.Type = data.Type
	existingData.Content = data.Content
	existingData.Status = data.Status
	existingData.UpdatedAt = time.Now().Unix()
	return data, s.DB.Save(existingData).Error
}

/**
 * @Description: 删除
 * @param id
 * @return error
 */
func (s *SysContentService) DeleteSysContent(id uint) error {
	var data models.SysContent
	if err := s.DB.Where("id = ?", id).First(&data).Error; err != nil {
		return errors.New("数据不存在")
	}
	return s.DB.Delete(&data).Error
}

/**
 * @Description: 批量删除
 * @param id
 * @return error
 */
func (s *SysContentService) DeleteSysContents(ids []string) error {
	for _, id := range ids {
		var data models.SysContent
		if err := s.DB.Where("id = ?", id).First(&data).Error; err != nil {
			return errors.New("数据不存在")
		}
		err := s.DB.Delete(&data).Error
		if err != nil {
			return errors.New("删除失败")
		}
	}
	return nil
}
